package vn.pyco.prez.tapestry.components;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionAttribute;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.apache.tapestry5.tree.TreeModel;
import org.apache.tapestry5.tree.TreeNode;

import vn.pyco.prez.PrezConstants;
import vn.pyco.prez.PrezConstants.Steps;
import vn.pyco.prez.tapestry.model.FileTreeModel;
import vn.pyco.prez.tapestry.model.FileTreeModelAdapter;
import vn.pyco.prez.tapestry.model.FileValueEncoder;
import vn.pyco.prez.tapestry.pages.Index;

@Import(library={
		"${prez.scripts}/bootstrap/bootstrap-transition.js",
		"${prez.scripts}/bootstrap/bootstrap-modal.js"
})
public class PrezSelector {
	@Inject
	private JavaScriptSupport _jsSupport;

	@Inject
	private Request _request;
	
	@Inject
	private AjaxResponseRenderer _ajaxResponseRenderer;
	
	@Property(write=false)
	@Persist
	private List<File> _roots;
	
	@Persist
	private File _selectedRoot;
	
	@Property
	@SessionAttribute(PrezConstants.SESSION_SELECTED_FILE)
	private File _selectedFile;
	
	@SuppressWarnings("unused")
	@SessionAttribute(PrezConstants.SESSION_STEP)
	private Steps _currentStep;
	
	@Inject
	@Property
	private Block _browser;

	@InjectComponent
	private Zone _treeZone;
	
	@InjectComponent
	private Zone _selectedZone;
	
	@Property
	private File _currentDrive;
	
	@Property
	private TreeNode<File> _currentNode;
	
	/* ------------------- RENDER PHASE ------------------------*/
	
	@SetupRender
	public void setupRender() {
    	_roots = Arrays.asList(File.listRoots());
    	_selectedRoot = _roots.get(0);
	}
	
	@AfterRender
	public void afterRender(){
		StringBuffer sb = new StringBuffer();
		sb.append("$('#fileBrowser').modal({ \n");
		sb.append("	show:false \n");
		sb.append("}); \n");
		sb.append("$('#fileBrowser').appendTo('body');\n");
		_jsSupport.addScript(sb.toString());
	}
	
	/* ------------------- GETTERS ------------------------*/

	public TreeModel<File> getDirectories(){
		return new FileTreeModel(new FileValueEncoder(), new FileTreeModelAdapter("ppt,pptx,odp"), _selectedRoot);
	}
	
	public String getButtonClass(){
		String css = "btn";
		if(_currentDrive.equals(_selectedRoot) ) {
			css += " btn-primary";
		}
		return css;
	}

	public String getLeafClass() {
		String css = "t-tree-label t-selectable";
	    if (_selectedFile != null && _currentNode.getValue().equals(_selectedFile)) {
	    	css += " t-selected-leaf-node-label";
	    }
	    return css;
	}
	
	/* ------------------- EVENTS ------------------------*/ 

	@OnEvent(value="selectdrive")
	public Block selectDrive(String path){
		File newDrive = new File(path);
		for (File file : _roots) {
			if(file.equals(newDrive)) {
				_selectedRoot = file;
				break;
			}
		}
		return _browser;
	}
	
	@OnEvent(value="leafselected")
	public void leafSelected(String filePath) {
		_selectedFile = new File(filePath);
	    if (_request.isXHR()) {
	        _ajaxResponseRenderer.addRender(_treeZone).addRender(_selectedZone);
	    }
	}
	
	@OnEvent(value="generateprez")
	public Class<?> generate() {
		if(_selectedFile != null) {
			_currentStep = Steps.PROCESS;
		} else {
			_currentStep = Steps.SELECT;
		}
		return Index.class;
	}
}
